<?php

class faculty_IndexController extends Zend_Controller_Action
{

 public function init()
    {
        /* Initialize action controller here */
    	defined('BASE_URL')	|| define('BASE_URL', Zend_Controller_Front::getInstance()->getBaseUrl());
    }

    public function indexAction()
    {
    	//$this->_helper->layout()->disableLayout();//disableLayout
    	$db = new faculty_Model_DbTable_DbFac();
    	if($this->getRequest()->isPost()){
	    	if($this->getRequest()->getParam("edit")){
				$id = $this->getRequest()->getParam("chk_modify");
				//print_r($id); exit;
				if(!empty($id)){
					$this->_redirect("/faculty/fac/edit-faculty/id/".$id);
					$frm = new faculty_Form_FrmFac();
		 			$row = $db->getfac($id);			
					$dbfaculty = $frm->fromfac($row);
					$this->view->frmfaculty= $dbfaculty;
				}
				$this->_redirect("/faculty/fac/index");
	    	}
	    	if($this->getRequest()->getPost("delete")){
	    		$id = $this->getRequest()->getParam("chk_modify");
	    		//print_r($id); exit;
	    		if(!empty($id)){
	    			$db->deletefac($id);    	
	    			$this->_redirect("faculty/fac/index");
	    		}
	    		$this->_redirect("faculty/fac/index");
	    	}
    	}	
    	$fac= $db->selectfac();
    	$this->view->viewfac = $fac;
     	
    }
    public function listAction()
    {
    	$this->_helper->layout()->disableLayout();//disableLayout
    	$db = new faculty_Model_DbTable_DbFac();
    	if($this->getRequest()->isPost()){
    		if($this->getRequest()->getParam("edit")){
    			$id = $this->getRequest()->getParam("chk_modify");
    			//print_r($id); exit;
    			if(!empty($id)){
    				$this->_redirect("/faculty/index/edit/id/".$id);
    				$frm = new faculty_Form_FrmFac();
    				$row = $db->getfac($id);
    				$dbfaculty = $frm->fromfac($row);
    				$this->view->frmfaculty= $dbfaculty;
    			}
    			else {
					echo "<script type='text/javascript'> alert('Please Try Again'); </script>";
				}
    		}
    		if($this->getRequest()->getPost("delete")){
    			$id = $this->getRequest()->getParam("chk_modify");
    			//print_r($id); exit;
    			if(!empty($id)){
    				$db->deletefac($id);
    				$this->_redirect("faculty/index/index");
    			}
    			else {
					echo "<script type='text/javascript'> alert('Please Try Again'); </script>";
				}
    		}
    		if($this->getRequest()->getPost("status")){
    			$id = $this->getRequest()->getParam("status");
    			//print_r($id);exit();
    			if(!empty($id)){
    				$dbfaculty = $db->updateUnStatus($id);
    				//echo "<script type='text/javascript'> alert('Update Status Faculty Successfull'); </script>";
    			}
    			//$this->_redirect("faculty/fac/index");
    		}
    		if($this->getRequest()->getPost("unstatus")){
    			$id = $this->getRequest()->getParam("unstatus");
    			//print_r($id);exit();
    			$dbfaculty = $db->updateStatus($id);
    			//echo "<script type='text/javascript'> alert('Update Status Faculty Successfull'); </script>";
    		}
    	}
    	$fac= $db->selectfac();
    	$this->view->viewfac = $fac;
    }
    public function addAction()
    {
    //$this->_helper->layout()->disableLayout();//disableLayout
    if($this->getRequest()->isPost()){
    	$db = new faculty_Model_DbTable_DbFac();
    	$data = $this->getRequest()->getPost();
    	//print_r($data);exit();
    	if($this->getRequest()->getParam("save_colse")){
    		if($this->getRequest()->getParam("fac_name") != ""){
	    		$dbfaculty = $db->insertFac($data);
	    		echo "<script type='text/javascript'> alert('Insert Faculty Successfull'); </script>";
	    		$this->_redirect("/faculty");
    		}
    		else {
    			echo "<script type='text/javascript'> alert('Please Try Again'); </script>";
    		}
    	}
    	elseif($this->getRequest()->getParam("save_new")){
    	if($this->getRequest()->getParam("fac_name") != ""){
	    		$dbfaculty = $db->insertFac($data);
	    		echo "<script type='text/javascript'> alert('Insert Faculty Successfull'); </script>";
	    		$this->_redirect("/faculty/index/add");
    		}
    		else {
    			echo "<script type='text/javascript'> alert('Please Try Again'); </script>";
    			
    		}
    	}
    }	
    	$dbfac = new faculty_Form_FrmFac;
    	$dbfaculty = $dbfac->fromfac();
    	$this->view->frmfaculty= $dbfaculty;
    }
    public function editAction()
    {   
//     	/* Initialize action controller here */
   // $this->_helper->layout()->disableLayout();		
		$dbuser = new faculty_Model_DbTable_DbFac();
		if($this->getRequest()->isPost()){
			$data = $this->getRequest()->getPost();
			//print_r($data);exit();
			if($this->getRequest()->getParam("save_colse")){
				if($data != ""){
				$dbfaculty = $dbuser->updateFac($data);
				echo "<script type='text/javascript'> alert('Update Status Faculty Successfull'); </script>";
				$this->_redirect("/faculty/index/list");
				}
			}
			elseif($this->getRequest()->getParam("save_new")){
				$dbfaculty = $dbuser->updateFac($data);
				echo "<script type='text/javascript'> alert('Update Status Faculty Successfull'); </script>";
				$this->_redirect("/faculty/index/add");
			}
			elseif ($this->getRequest()->getParam("save")){
				$dbfaculty = $dbuser->updateFac($data);
				echo "<script type='text/javascript'> alert('Update Status Faculty Successfull'); </script>";
				$id = $this->getRequest()->getParam("id");				
				$row = $dbuser->getfac($id);
				//print_r($row);exit();
				$frm = new faculty_Form_FrmFac();
				$dbfaculty = $frm->fromfac($row);
				$this->view->frmfaculty= $dbfaculty;
			}
			elseif ($this->getRequest()->getParam("cancel")) {
				//print_r($data);exit();
				$this->_redirect("/faculty/");
			}
			else{
				echo "<script type='text/javascript'> alert('Please Try Again'); </script>";
			}
		}
  		$id = $this->getRequest()->getParam("id");
 		$frm = new faculty_Form_FrmFac();
 		$row = $dbuser->getfac($id);	
		$dbfaculty = $frm->fromfac($row);
		$this->view->frmfaculty= $dbfaculty;
		
    }
    public function deleteAction(){
    	$id = $this->getRequest()->getParam("id");
    	//print_r($id);exit();
    	$db = new faculty_Model_DbTable_DbFac();
    	$db->deletefac($id);    	
    	$this->_redirect("faculty/index/list");
    }
	
}